package com.work.keep.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;

/**
 * Created by dllo on 16/1/17.
 */
public class PullToRefreshOrLoadBaseLayout<T extends View> extends LinearLayout implements LayoutRefreshableAndLoadable {
    /**
     * 阻尼系数
     */
    private static final float OFFSET_RADIO = 2.5F;
    /**
     * 下拉刷新 和上拉加载的监听器
     */
    private OnRefreshListener<T> onRefreshListener;

    /**
     * 下拉刷新的布局管理器
     */
    private StateLayoutManager headRefreshManager;
    private int headerHeight;
    /**
     * 上拉加载的布局管理器
     */
    private StateLayoutManager footLoadManager;
    private int footerHeight;

    /**
     * 上拉加载与下来刷新的开关 初始位置时上拉刷新可用 下拉加载不可用
     */
    private boolean mPullRefreshEnable = true;

    private boolean mPullLoadEnable = false;
    //滑到底部加载是否可用
    private boolean mScrollLoadEnable = false;
    /**
     * 是否截断touch事件
     */
    private boolean mInterceptEventEnable = true;
    /**
     * 表示是否消费了touch事件，如果是，则不调用父类的onTouchEvent方法
     */
    /**
     * 移动点的保护范围值
     */
    private int mTouchSlop;
    private boolean mIsHandledTouchEvent = false;
    /**
     * 下拉的状态
     */
    private LayoutShowLoadingState.LoadState mPullDownState = LayoutShowLoadingState.LoadState.NONE;
    /**
     * 上拉的状态
     */
    private LayoutShowLoadingState.LoadState mPullUpState = LayoutShowLoadingState.LoadState.NONE;
    /**
     * 可以下拉刷新的View
     */
    T mRefreshableView;
    /**
     * 平滑滚动的Runnable
     */
    private SmoothScrollRunnable mSmoothScrollRunnable;
    final class SmoothScrollRunnable implements Runnable {
        /**
         * 动画效果
         */
        private final Interpolator mInterpolator;
        /**
         * 结束Y
         */
        private final int mScrollToY;
        /**
         * 开始Y
         */
        private final int mScrollFromY;
        /**
         * 滑动时间
         */
        private final long mDuration;
        /**
         * 是否继续运行
         */
        private boolean mContinueRunning = true;
        /**
         * 开始时刻
         */
        private long mStartTime = -1;
        /**
         * 当前Y
         */
        private int mCurrentY = -1;

        /**
         * 构造方法
         *
         * @param fromY    开始Y
         * @param toY      结束Y
         * @param duration 动画时间
         */
        public SmoothScrollRunnable(int fromY, int toY, long duration) {
            mScrollFromY = fromY;
            mScrollToY = toY;
            mDuration = duration;
            mInterpolator = new DecelerateInterpolator();
        }

        @Override
        public void run() {
            /**
             * If the duration is 0, we scroll the view to target y directly.
             */
            if (mDuration <= 0) {
                setScrollTo(0, mScrollToY);
                return;
            }

            /**
             * Only set mStartTime if this is the first time we're starting,
             * else actually calculate the Y delta
             */
            if (mStartTime == -1) {
                mStartTime = System.currentTimeMillis();
            } else {

                /**
                 * We do do all calculations in long to reduce software float
                 * calculations. We use 1000 as it gives us good accuracy and
                 * small rounding errors
                 */
                final long oneSecond = 1000;    // SUPPRESS CHECKSTYLE
                long normalizedTime = (oneSecond * (System.currentTimeMillis() - mStartTime)) / mDuration;
                normalizedTime = Math.max(Math.min(normalizedTime, oneSecond), 0);

                final int deltaY = Math.round((mScrollFromY - mScrollToY)
                        * mInterpolator.getInterpolation(normalizedTime / (float) oneSecond));
                mCurrentY = mScrollFromY - deltaY;

                setScrollTo(0, mCurrentY);
            }

            // If we're not at the target Y, keep going...
            if (mContinueRunning && mScrollToY != mCurrentY) {
                PullToRefreshOrLoadBaseLayout.this.postDelayed(this, 16);// SUPPRESS CHECKSTYLE
            }
        }

        /**
         * 停止滑动
         */
        public void stop() {
            mContinueRunning = false;
            removeCallbacks(this);
        }
    }

    private void setScrollTo(int i, int mScrollToY) {

    }

    public PullToRefreshOrLoadBaseLayout(Context context) {
        this(context, null);
    }

    public PullToRefreshOrLoadBaseLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToRefreshOrLoadBaseLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void setPullDownRefreshEnabled(boolean pullDownRefreshEnabled) {

    }

    @Override
    public void setPullUpLoadingEnabled(boolean pullUpLoadingEnabled) {

    }

    @Override
    public void setAutoLoadingEnabled(boolean autoLoadingEnabled) {

    }

    @Override
    public boolean isReadyForRefresh() {
        return false;
    }

    @Override
    public boolean isReadyForLoading() {
        return false;
    }

    @Override
    public boolean isReadyForAutoLoading() {
        return false;
    }

    @Override
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {

    }

    @Override
    public void refreshComplete() {

    }

    @Override
    public void loadingComplete() {

    }

    @Override
    public StateLayoutManager getRefreshView() {
        return null;
    }

    @Override
    public StateLayoutManager getLoadingView() {
        return null;
    }


    @Override
    public void setLastUpdatadlabel(CharSequence label) {

    }
}
